package org.example;

import com.google.common.collect.Maps;
import org.example.chrome.ChromeUtils;
import org.example.ui.MainBox;
import org.example.ui.UserAgentSelectBox;
import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.Map;

public class Main {
    public static WebDriver DRIVER;

    public static void UI() throws IOException, InterruptedException {
        MainBox jf = new MainBox();
        // 创建组件
        JButton btn1 = new JButton("打开浏览器");
        JButton btn2 = new JButton("关闭浏览器");
        JButton btn3 = new JButton("按钮3");
        btn1.setBackground (Color.red);
        btn1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    openChrome();
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
//        btn2.setPreferredSize(new Dimension(100,100));
        btn2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DRIVER.quit();
            }
        });
        // 添加到窗体上
        jf.add(btn1);
        jf.add (btn2);
        jf.add (btn3);

        UserAgentSelectBox userAgentSelectBox = new UserAgentSelectBox(jf);
        jf.setVisible (true);
    }

    public static void main(String[] args) throws Exception {
        MainBox jf = new MainBox();
        UserAgentSelectBox userAgentSelectBox = new UserAgentSelectBox(jf);
        JButton openBtn = new JButton("打开浏览器");
        openBtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                try {
                    openChrome();
                } catch (InterruptedException ex) {
                    throw new RuntimeException(ex);
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });
        jf.add (openBtn);

        jf.setVisible (true);
    }
    public static void openChrome() throws InterruptedException, IOException {
        // 不指定路径，默认从 classPath中查找
//        System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
        ChromeOptions options = new ChromeOptions();
        // 设置代理
        Proxy proxy = new Proxy();
        proxy.setHttpProxy("27.159.184.186:26957")
                .setSslProxy("27.159.184.186:26957");

//        proxy.setProxyType(Proxy.ProxyType.MANUAL);
//        proxy.setSocksProxy("E8CE0AB0:3CF705D6CCE8@114.233.49.175:49305");
//        proxy.setSocksVersion(5);
//        proxy.setSocksUsername("E8CE0AB0");
//        proxy.setSocksPassword("3CF705D6CCE8");

        options.setProxy(proxy);

        // 浏览器参数
//        options.addArguments("window-size=1000,800");

        // 设备信息
        Map<String, String> mobileEmulation = Maps.newHashMap();
        mobileEmulation.put("userAgent", "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19");
        options.setExperimentalOption("mobileEmulation", mobileEmulation);

        options.addExtensions(new File("/Users/gongguojun/javaprojects/chromeTools/chrome-multipass-0.7.4.crx"));

        DRIVER = new RemoteWebDriver(ChromeUtils.SERVICE.getUrl(), options);

        DRIVER.get("chrome-extension://enhldmjbphoeibbpdhmjkchohnidgnah/options.html");
        DRIVER.findElement(By.id("url")).sendKeys("https://ip.cn/api/index?ip=&type=0");
        DRIVER.findElement(By.id("username")).sendKeys("597C6FF2");
        DRIVER.findElement(By.id("password")).sendKeys("83C9FCA3E4B4");
        DRIVER.findElement(By.className("credential-form-submit")).click();


        DRIVER.get("https://ip.cn/api/index?ip=&type=0");
    }
}